var addLindID = "addQuestionDetail";     			//增加行按扭ID
var deleteLindID = "deleteAllQuestionDetail";		//删除所有行按扭ID
var tableID = "tableQuestionDetail";				//表格ID

$(function(){
	$('#' + addLindID).click(function(){
		bindQuestionList(null);
	});
	
	$('#' + deleteLindID).click(function(){
		$('#' + tableID).find('tbody').find('tr').not('.no-records-found').remove();
		
		if($('#' + tableID).find('tbody').children('tr').length == 0){
			var trStr = '<tr class="no-records-found">	'+
					    '   <td colspan="5" style="font-size:14px;">			'+
						'		单击底部“新增行”增加问卷问题！		'+
						'	</td>						'+
						'</tr>';
			$('#' + tableID).find('tbody').append(trStr);
		}
	});
});

function bindQuestionList(data){
	$('#' + tableID).find('.no-records-found').remove();
	
	var index = 1;
	if($('#' + tableID).find('tbody').find('tr').length > 0){
		index = parseInt($('#' + tableID).find('tbody').find('tr:last').find('.serialnumber').val()) + 1;
		
		var questionEle = $('#' + tableID).find('tbody').find('tr:last').find('[name="QUESTION"]');
		var val1 = questionEle.val();
		if("" == val1){
			window.wxc.xcConfirm('请输入问题', window.wxc.xcConfirm.typeEnum.error);
			return ;
		}
		
		var questionitemEle = $('#' + tableID).find('tbody').find('tr:last').find('[name="QUESTIONITEM"]');
		val1 = questionitemEle.val();
		if("" == val1){
			window.wxc.xcConfirm('请输入问题选项', window.wxc.xcConfirm.typeEnum.error);
			return ;
		}
	}
    
    var questionid = "";
    var question = "";
    var questiontype = "";
    var questionitem = "";
    
    if(data != null){
    	questionid = data.QUESTIONID;
    	question = data.QUESTION;
    	questiontype = data.QUESTIONTYPE;
    	questionitem = data.QUESTIONITEM;
    }
    
    var selectHtml = '<select type="text" class="form-control" name="QUESTIONTYPE">';
    var questionTypeStr = bindDict("", "question_item", "", null);
    for (var i = 0; i < questionTypeStr.length; i++) {
		var value = questionTypeStr[i].itemcode;
		var text = questionTypeStr[i].itemname;
		if(questiontype != "" && (parseInt(questiontype) == parseInt(value))){
			selectHtml += '<option value="' + value + '" selected = "selected">' + text + '</option>';
		} else {
			selectHtml += '<option value="' + value + '">' + text + '</option>';
		}
	}
    selectHtml += '</select>';
	var trStr = '<tr id="tr_'+ index +'" class="data">'+
				'<form class="form-horizontal dictDetailForm" id="dictDetailForm_'+ index +'">'+
			    '    <td>'+
			    '    	<input type="hidden" class="form-control" name="QUESTIONID" value="'+ questionid +'" />'+												        
			    '    	<input type="number" class="form-control serialnumber" style="text-align: center;" value="'+ index +'"/>'+
			    '    </td>'+
			    '    <td>'+
			    '    	<input type="text" class="form-control" name="QUESTION" value="'+ question +'" />'+
		        '	</td>'+
			    '    <td style="width:120px;">' + selectHtml + '</td>'+
			    '    <td style="width:40%">'+
			    '    	<input type="text" id="questionitem_' + index + '" name="QUESTIONITEM" value="'+ questionitem +'" placeholder="输入每一段文字代表一个选项" />'+
			    '    </td>'+
			    '    <td>'+
    			'		<button type="button" class="Delete btn btn-danger btn-sm" onclick="deleteDetailLine(\'tr_'+ index +'\');" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span></button>'+
			    '    </td>'+
			    '</form>'+
			    '</tr>';
	
	$('#' + tableID).find('tbody').append(trStr);
	$('#questionitem_' + index).tagsinput();
	
}

function deleteDetailLine(trId){
	$('#' + trId).remove();
	
	if($('#' + tableID).find('tbody').children('tr').length == 0){
		var trStr = '<tr class="no-records-found">	'+
				    '   <td colspan="5" style="font-size:14px;">			'+
					'		单击底部“新增行”增加问卷问题！		'+
					'	</td>						'+
					'</tr>';
		$('#' + tableID).find('tbody').append(trStr);
	}
}
